package com.example.cookiedemo.controller;

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

@RestController
public class CookieController {

    @GetMapping("/set-cookie")
    public String setCookie(HttpServletResponse response, HttpServletRequest request) {

        Cookie[] cookies = request.getCookies();

        // 2. 检查是否包含目标Cookie（如"sessionId"）
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals("xtermLogExpirationTime")) {
                    System.out.println(cookie.getValue());
                }

            }
        }

        String cookieValue = "xtermLogExpirationTime=1761227738816; Expires=Thu, 23 Oct 2026 14:13:48 GMT; Path=/; Secure;";
        String cookie2 = "xtermLogExpirationTime=1761227738816; Expires=Thu, 23 Oct 2026 14:13:48 GMT; Path=/; Secure; SameSite=None";
        response.addHeader("Set-Cookie", cookieValue);
        response.addHeader("Set-Cookie", cookie2);

        return "Cookie已成功设置！Token值：";
    }

    @GetMapping("/hello")
    public String hello() {
        return "欢迎访问Spring Boot Cookie示例应用！请访问 /set-cookie 来设置Cookie。";
    }
}